package com.adprof.demo.natives.recyclerview;


import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.adprof.demo.android.R;
import com.adprof.demo.utils.ConstantsDemo;
import com.adprof.sdk.api.AdError;
import com.adprof.sdk.api.AdRequest;
import com.adprof.sdk.api.NativeAd;
import com.adprof.sdk.api.NativeAdData;
import com.adprof.sdk.api.NativeAdLoadListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

public class NativeRecyclerviewDemoActivity extends AppCompatActivity {

    private static final int NORMAL_ITEM_COUNT = 8;

    private NativeRecyclerAdapter myAdapter;

    private NativeAd nativeAd;

    private final List<NativeAdData> mData = new ArrayList<>();

    private boolean isLoading = false;

    private RecyclerView recyclerView;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo_activity_native_ad_recycle);
        recyclerView = findViewById(R.id.recyclerView);
        progressBar = findViewById(R.id.progressBar);
        initView();
        loadRecyclerAd();
    }

    private void initView() {
        final LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        myAdapter = new NativeRecyclerAdapter(this, mData);
        recyclerView.setAdapter(myAdapter);

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                if (RecyclerView.SCROLL_STATE_IDLE == newState) {
                    int visibleItemCount = layoutManager.getChildCount();
                    int totalItemCount = layoutManager.getItemCount();
                    int pastVisibleItems = layoutManager.findFirstVisibleItemPosition();
                    if (!isLoading && (visibleItemCount + pastVisibleItems) >= totalItemCount) {
                        loadRecyclerAd();
                    }
                }
            }
        });
    }

    private void showLoading(boolean show) {
        isLoading = show;
        progressBar.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    private void loadRecyclerAd() {
        Log.d(ConstantsDemo.LOG_TAG, "-----------loadListAd-----------");
        showLoading(true);

        String adUnitID = ConstantsDemo.EXPRESS_SLOTID;

        AdRequest adRequest = new AdRequest.Builder()
                .setCodeId(adUnitID)
                .setIsExpressAd(ConstantsDemo.expressAD(adUnitID))
                .setExtOption(new HashMap<>()).build();

        nativeAd = new NativeAd(adRequest, new NativeAdLoadListener() {
            @Override
            public void onAdError(AdError error) {
                Log.d(ConstantsDemo.LOG_TAG, "----------onAdError----------:" + error.toString() + ":" + adUnitID);
                Toast.makeText(NativeRecyclerviewDemoActivity.this, "onError:" + error.toString(), Toast.LENGTH_SHORT).show();
                showLoading(false);
            }

            @Override
            public void onAdLoad(List<NativeAdData> adDataList) {
                showLoading(false);

                if (nativeAd.isReady() && adDataList != null && !adDataList.isEmpty()) {
                    Log.d(ConstantsDemo.LOG_TAG, "----------onAdLoad----------:" + adDataList.size());
                    for (final NativeAdData adData : adDataList) {
                        Collections.addAll(mData, new NativeAdData[NORMAL_ITEM_COUNT]);
                        mData.add(adData);
                        Collections.addAll(mData, new NativeAdData[NORMAL_ITEM_COUNT]);
                    }

                    myAdapter.notifyDataSetChanged();
                }
            }
        });

        nativeAd.loadAd();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mData.clear();
        if (nativeAd != null) {
            nativeAd.destroyAd();
            nativeAd = null;
        }
    }

}